/*
 * Copyright (c) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.data;

import com.google.api.client.googleapis.GoogleUrl;
import com.google.api.client.xml.XmlNamespaceDictionary;
import com.google.api.client.xml.atom.AtomFeedContent;
import com.google.api.data.gdata.v2.model.Entry;
import com.google.api.data.gdata.v2.model.batch.BatchFeed;

import java.io.IOException;

/**
 * Simple service for the Google Data API adding some convenient methods.
 * 
 * @author Nicolas Garnier (nivco@google.com)
 * @since 2.2
 */
public class ExtendedGenericAtomService extends GenericAtomService {

  // ============= Constructors ================

  /**
   * Constructs a Google API service.
   * 
   * @param transport Contains the transport and authentication informations.
   */
  public ExtendedGenericAtomService(String applicationName, XmlNamespaceDictionary namespaceDict) {
    super(applicationName, namespaceDict);
  }

  // ============= Methods ================

  /**
   * Updates the given {@code entry}.
   * 
   * @param entry
   * @return
   * @throws IOException
   */
  public <EntryType extends Entry> EntryType executeUpdate(EntryType entry) throws IOException {
    return super.executeUpdate(entry, new GoogleUrl(entry.getEditLink()), entry.etag);
  }

  /**
   * Only sends out the differences between the given {@code updated} and
   * {@code original} entries.
   * 
   * @param updated
   * @param original
   * @return
   * @throws IOException
   */
  public <EntryType extends Entry> EntryType executePatchRelativeToOriginal(EntryType updated,
      EntryType original) throws IOException {
    return super.executePatchRelativeToOriginal(updated, original,
        new GoogleUrl(original.getEditLink()), updated.etag);
  }

  /**
   * Deletes the given {@code entry}.
   * 
   * @param entry The entry to delete.
   * @throws IOException
   */
  public void executeDelete(Entry entry) throws IOException {
    super.executeDelete(new GoogleUrl(entry.getEditLink()), entry.etag);
  }

  /**
   * Executes the given batch request.
   * 
   * @param batchfeed The Batch feed containing all the operations to execute
   * @param feedBatchUrl The URL where to post the batch feed
   * @return The batch feed response
   * @throws IOException
   */
  @SuppressWarnings("unchecked")
  public <BatchFeedType extends BatchFeed> BatchFeedType executeBatch(BatchFeedType batchfeed,
      GoogleUrl feedBatchUrl) throws IOException {
    AtomFeedContent content = new AtomFeedContent();
    content.namespaceDictionary = namespaceDictionary;
    content.feed = batchfeed;
    return (BatchFeedType) executeInsert(content, feedBatchUrl, batchfeed.getClass());
  }
}
